package com.xxx.meter.ui.info.web;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.xxx.meter.R;
import com.xxx.meter.base.BaseActivity;
import com.xxx.meter.model.http.Api;
import com.xxx.meter.model.http.ApiCallback;
import com.xxx.meter.model.http.bean.WorkInfoBean;
import com.xxx.meter.model.http.bean.base.PageBean;
import com.xxx.meter.model.utils.ButtonUtil;
import com.xxx.meter.model.utils.KeyBoardUtil;
import com.xxx.meter.ui.info.adapter.InfoFindAdapter;
import com.xxx.meter.ui.info.web.WebActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class InfoFindWebActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R.id.main_recycler)
    RecyclerView mRecycler;
    @BindView(R.id.main_refresh)
    SwipeRefreshLayout mRefresh;
    @BindView(R.id.not_data)
    ImageView mNotData;
    @BindView(R.id.not_net)
    ImageView mNotNet;
    @BindView(R.id.main_title)
    TextView mTitle;

    @BindView(R.id.work_find_edit)
    EditText mEdit;

    private int page = 1;
    private InfoFindAdapter mAdapter;
    private ArrayList<WorkInfoBean> mList = new ArrayList<>();

    private String searchText;
    private int type;
    private int subType;
    private String machineType;
    private String machineId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_work_find;
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        mTitle.setText(intent.getStringExtra("title"));

        type = intent.getIntExtra("type", 0);
        subType = intent.getIntExtra("subType", 0);
        machineType = intent.getStringExtra("machineType");
        machineId = intent.getStringExtra("machineId");

        mAdapter = new InfoFindAdapter(mList);
        mRecycler.setLayoutManager(new LinearLayoutManager(this));
        mRecycler.setAdapter(mAdapter);
        mRefresh.setOnRefreshListener(this);
        mAdapter.setOnLoadMoreListener(this, mRecycler);
        mAdapter.setOnItemClickListener(this);

        loadWebInfo(true);
    }


    @OnClick({R.id.main_return, R.id.work_find_search, R.id.not_data, R.id.not_net})
    public void OnClick(View view) {
        if (ButtonUtil.isFastClick()) return;
        KeyBoardUtil.closeKeyBord(this, mEdit);
        switch (view.getId()) {
            case R.id.main_return:
                finish();
                break;
            case R.id.not_data:
                loadWebInfo(true);
                break;
            case R.id.not_net:
                loadWebInfo(true);
                break;
            case R.id.work_find_search:
                loadWebInfo(true);
                break;
        }
    }

    @OnTextChanged(R.id.work_find_edit)
    public void onTextChanged(CharSequence charSequence) {
        searchText = charSequence.toString();
        loadWebInfo(false);
    }

    @Override
    public void onRefresh() {
        page = 1;
        loadWebInfo(false);
    }

    @Override
    public void onLoadMoreRequested() {
        page++;
        loadWebInfo(false);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        KeyBoardUtil.closeKeyBord(this, mEdit);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if (ButtonUtil.isFastClick()) return;
        Intent intent = new Intent(this, WebActivity.class);
        intent.putExtra("url", mList.get(position).getUrl());
        intent.putExtra("title", mList.get(position).getName());
        startActivity(intent);
    }


    /**
     * @param isRefresh 是否刷新
     * @Model 获取机器巡查数据
     */
    private void loadWebInfo(boolean isRefresh) {
        Api.getInstance().getWebInfo(searchText, type, subType, machineType, machineId, page, 20)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ApiCallback<PageBean<WorkInfoBean>>(this, isRefresh) {
                    @Override
                    public void onSuccess(PageBean<WorkInfoBean> pageBean) {
                        List<WorkInfoBean> list;
                        if (pageBean == null || (list = pageBean.getList()) == null || list.size() == 0) {
                            mRefresh.setVisibility(View.GONE);
                            mNotNet.setVisibility(View.GONE);
                            mNotData.setVisibility(View.VISIBLE);
                            mRefresh.setRefreshing(false);
                            mAdapter.loadMoreEnd(true);
                            return;
                        }

                        mRefresh.setVisibility(View.VISIBLE);
                        mNotData.setVisibility(View.GONE);
                        mNotNet.setVisibility(View.GONE);
                        if (page == 1) {
                            mList.clear();
                        }
                        mList.addAll(list);
                        if (list.size() < 20) {
                            mAdapter.loadMoreEnd(true);
                        } else {
                            mAdapter.loadMoreComplete();
                        }
                        mRefresh.setRefreshing(false);
                        mAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onError(int errorCode, String errorMessage) {
                        mRefresh.setVisibility(View.GONE);
                        mNotData.setVisibility(View.GONE);
                        mNotNet.setVisibility(View.VISIBLE);
                        mRefresh.setRefreshing(false);
                    }
                });
    }

}
